const mongoose = require('mongoose');
const fs = require('fs');
const io = require('../../../server.js').io;

exports.register = register;
exports.login = login;
exports.reAudit = reAudit;
exports.getLogin = getLogin;
exports.exit = exit;

let Merchant = mongoose.model('Merchant');
function register(req, resp, next) {
    let body = req.body;
    let files = req.files;

    let localPath = 'D:/o2o/images';
    let remotePath = 'http://10.222.29.156:3456/images';

    idCardPath = '/idCard/' + body.username + '-' + Date.now() + '.jpg';
    portraitPath = '/portrait/' + body.username + '-' + Date.now() + '.jpg';

    fs.writeFile(localPath + idCardPath, files.idCard[0].buffer, function (err) {
        if (err)
            console.error(err);
    });
    fs.writeFile(localPath + portraitPath, files.portrait[0].buffer, function (err) {
        if (err)
            console.error(err);
    });
    body.idCard = remotePath + idCardPath;
    body.portrait = remotePath + portraitPath;

    let merchant = new Merchant(body);
    merchant.status = '未激活';
    // save to db and response
    merchant.save(function (error, result) {
        if (error) {
            resp.json({
                error: error.message,
                message: null
            })
            return;
        }
        resp.json({
            error: null,
            message: result
        });

    });
}

function login(req, resp, next) {
    let body = req.body;
    Merchant.findOne({ username: body.username, password: body.password }, function (error, result) {
        if (error) {
            resp.json({
                error: error.message,
                message: null
            });
            return;
        }
        if (!result) {
            resp.json({
                error: 'username or password error',
                message: null
            });
            return;
        }
        req.session.merchant = result;
        resp.json({
            error: null,
            message: result
        })
    });
}

function reAudit(req, resp, next) {
    let body = req.body;
    let files = req.files;

    let localPath = 'D:/o2o/images';
    let remotePath = 'http://10.222.29.156:3456/images';

    idCardPath = '/idCard/' + body.username + '-' + Date.now() + '.jpg';
    portraitPath = '/portrait/' + body.username + '-' + Date.now() + '.jpg';

    fs.writeFile(localPath + idCardPath, files.idCard[0].buffer, function (err) {
        if (err)
            console.error(err);
    });
    fs.writeFile(localPath + portraitPath, files.portrait[0].buffer, function (err) {
        if (err)
            console.error(err);
    });
    body.idCard = remotePath + idCardPath;
    body.portrait = remotePath + portraitPath;

    Merchant.update({ _id: body._id }, {
        $set: {
            name: body.name,
            idCard: remotePath + idCardPath,
            portrait: remotePath + portraitPath,
            address: body.address,
            status: '未激活',
            reason:null
        }
    }, function (err, raw) {
        if (err) {
            resp.json({
                error: err,
                message: null
            });
            return;
        }
        Merchant.findOne({
            _id: body._id
        }, function (error, res) {
            req.session.merchant = res;
            resp.json({
                error: null,
                message: res
            });
        });
    });

}

function getLogin(req, resp, next) {
    if (!req.session.merchant) {
        resp.json({
            error: 'you have not login',
            message: null
        });
        return;
    }
    Merchant.findOne({ _id: req.session.merchant._id }, function (error, result) {
        req.session.merchant = result;
        resp.json({
            error: null,
            message: req.session.merchant
        });
    });
}
function exit(req, resp, next) {
    req.session.merchant = null;
    resp.json({
        error: null,
        message: 'success'
    });
}